<?php

namespace App\Http\Controllers\V1\API;

use App\Http\Controllers\V1\API\BaseController;
use App\Models\V1\ScrollImages;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Log;

class ScrollImagesController extends BaseController
{
    /***
     * @return \Illuminate\Http\JsonResponse
     */
    public function getScrollImages() {
        $result = array(
            'errorCode' => Response::HTTP_NOT_FOUND,
            'errorMsg' => Response::$statusTexts[Response::HTTP_NOT_FOUND],
            'data' => array()
        );
        $images = $this->getScrollImageList();
        if ($images->isEmpty()) {
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = Response::$statusTexts[Response::HTTP_NO_CONTENT];
        }else {
            // 格式化数据
            $scrollImages = array();
            foreach ($images as $item) {
                $formatImage = array();
                $formatImage['url'] = $item['url'];
                $formatImage['redirectUrl'] = isset($item['redirect_url'])?$item['redirect_url']:'';
                array_push($scrollImages, $formatImage);
            }
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = Response::$statusTexts[Response::HTTP_OK];
            $result['data'] = $scrollImages;
        }
        return $this->sendResponse($result, $result['errorCode']);
    }

    /****
     * 获取所有栏目
     * @return \Illuminate\Support\Collection
     */
    private function getScrollImageList()
    {
        $data = ScrollImages::where('is_show',true)->orderBy('order','asc')->get();
        return $data;
    }
}
